package com.unnamed.collisions.geometry;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JPanel;
	
public class EdgePanel extends JPanel implements CollisionHandler{
	Point a,b,c,d;
	List<Polygon> polys;
	List<Point> intsec;
	Shapecosa sc;
	
	public void setPolygons(List<Polygon> polygons){
		this.polys = polygons;
	}
	
	public void setCosa(Shapecosa cosa){
		this.sc = cosa;
	}
	
	public EdgePanel(){
		super();
		a=new Point();
		b= new Point();
		c=new Point();
		d= new Point();
		
		intsec = new ArrayList<Point>();
		
		this.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				c.x = arg0.getX();
				c.y=arg0.getY();
				
			}
		});
		this.addMouseMotionListener(new MouseMotionListener() {
			
			@Override
			public void mouseMoved(MouseEvent arg0) {
				// TODO Auto-generated method stub
				if(c!=null){
					d.x=arg0.getX();
					d.y=arg0.getY();
				}
			}
			
			@Override
			public void mouseDragged(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
			
			@Override
			public boolean dispatchKeyEvent(KeyEvent arg0) {
				if(arg0.getKeyChar()=='n'){
					a.x = (float) (Math.random()*getWidth());
					a.y = (float) (Math.random()*getWidth());
					b.x = (float) (Math.random()*getWidth());
					b.y = (float) (Math.random()*getWidth());
					//System.out.println(a);
					//System.out.println(b);
					
					synchronized (sc) {
						polys.clear();
//						for(int c = 0;c<10;c++){
//							polys.add(createPolygon((float)Math.random()*800,(float)Math.random()*600, 3+(int)(Math.random()*5), 10+(int)(Math.random()*20)));
//						}
						
						try {
							//polys.add(HullTest.getTestHull());
							polys.addAll(HullTest.getHull());
							for(Polygon p:polys){
								p.translate(100, 100);
							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
	
					}
				}
				return false;
			}
		});
	}
	
	public Polygon createPolygon(float x, float y, int points, int radius){
		Point[] p = new Point[points];
		int pId = 0;
		float c=0;
		double step = 360/points;
		float rInc = radius;
		while(pId<points){
			float a = (float) Math.toRadians(c);
			float px = (float) Math.cos(a);
			float py = (float) Math.sin(a);
			
			System.out.println(String.format("a:%.3f (%.3f,%.3f)",c,px,py));
			p[pId++]=new Point(px*rInc,py*rInc);
			rInc+=3f;
			c+=step;
		}
		
		Polygon po = new Polygon(p);
		po.translate(x, y);
		
		return po;
	}
	
	public void paint(Graphics g){
		g.setColor(Color.black);
		g.clearRect(0, 0, getWidth(), getHeight());
		g.drawLine((int)a.x, (int)a.y, (int)b.x, (int)b.y);
		g.drawLine((int)c.x, (int)c.y, (int)d.x, (int)d.y);
		
		sc.collide();
//		Point i = Edge.intersects(a.x, a.y, b.x, b.y, c.x, c.y, d.x, d.y);
//		
//		if(i!=null){
//			g.drawRect((int)i.x-5, (int)i.y-5, 10, 10);
//		}
//		
//		if(polys!=null){
//			for(Polygon p:polys){
//				//p.translate(p.getX()+1f, p.getY()+1f);
//				p.rotate(0.1f);
//				
//				
//			}
//		}
		
		g.setColor(Color.black);
		
		long td = System.currentTimeMillis();
		
		if(polys!=null){
			for(Polygon p:polys){
				drawPoly(0,0,p, g);
			}
		}

		g.setColor(Color.blue);		
		td = System.currentTimeMillis();
		
		List<Collision> cl = sc.getCollisions();
		
		int np = 0;
		for(Collision col:cl){
			for(Point p:col.getPoints()){
				g.drawRect((int)p.x-5, (int)p.y-5, 10, 10);
				np++;
			}
		}
		
	}

	private void drawPoly(int xo, int yo,Polygon p, Graphics g){
		int length = p.points.length;
		for(int c = 0;c<length;c++){
			Point a = p.getTxPoint(c%length);
			Point b = p.getTxPoint((c+1)%length);
			g.setColor(Color.black);
			g.drawLine((int)a.x,(int)a.y,(int)b.x,(int)b.y);
			g.setColor(Color.green);
			g.drawRect((int)p.getCenter().x-5, (int)p.getCenter().y-5, 10, 10);
			float radius = p.getRadius();
			g.drawArc((int)(p.getCenter().x-radius), (int)(p.getCenter().y-radius), (int)p.getRadius()*2,(int)p.getRadius()*2, 0, 360);
			g.setColor(Color.pink);
			g.drawRect((int)p.getMassCenter().x-5, (int)p.getMassCenter().y-5, 10,10);
		}
	}
	public static void main(String[] args) {
		JDialog d = new JDialog();
		final EdgePanel panel = new EdgePanel();
		final Shapecosa sc = new Shapecosa();
		
		panel.setPolygons(sc.getPolygons());
		
		sc.setHandler(panel);
		panel.setCosa(sc);;
		d.add(panel);
		
		d.setBounds(0,0,800,600);
		
		Thread t = new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(!Thread.currentThread().isInterrupted()){
					long t0=System.currentTimeMillis();
					panel.repaint();
					try {
						Thread.sleep(60);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//System.out.println("Paint time:"+(System.currentTimeMillis()-t0));
				}
			}
		});

//		Thread t2 = new Thread(new Runnable() {
//			
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				while(!Thread.currentThread().isInterrupted()){
//					long t0=System.currentTimeMillis();
//					sc.collide();
//					try {
//						Thread.sleep(100);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					System.out.println("Collide time:"+(System.currentTimeMillis()-t0));
//				}
//			}
//		});
		
		t.start();
		//t2.start();
		d.setVisible(true);
		
	}

	@Override
	public void onCollisionDetected(Polygon a, Polygon b) {
		// TODO Auto-generated method stub
			//intsec.addAll(a.getIntersection(b));
			
		
	}
}
